    
----------------------------------------------------------------
-- Global Variables
----------------------------------------------------------------

UiModWindow = {}

UiModWindow.Settings = 
{
    showAdvancedWarning = true,
    showMainUiInModsList = false,
    
}

UiModWindow.isInErrorMode = false

UiModWindow.modsList = {}
UiModWindow.modsSets = {}
UiModWindow.modSetDisplayOrder = {}

UiModWindow.selectedModName = nil

UiModWindow.selectedTab = 2

UiModWindow.modOptionsChanged = false

-- Tabs are named based on the user settings.
UiModWindow.TAB_MAIN_UI_MODS = 1
UiModWindow.TAB_ADDONS_MODS  = 2

UiModWindow.modGroupTabs = {}
if( SystemData.Settings.UseCustomUI ) then   
    UiModWindow.modGroupTabs[UiModWindow.TAB_MAIN_UI_MODS ] = { name=GetPregameString( StringTables.Pregame.LABEL_CUSTOM_UI ) }
else
    UiModWindow.modGroupTabs[UiModWindow.TAB_MAIN_UI_MODS ] = { name=GetPregameString( StringTables.Pregame.LABEL_EA_DEFAULT ) }
end
UiModWindow.modGroupTabs[UiModWindow.TAB_ADDONS_MODS] = { name=GetPregameString( StringTables.Pregame.LABEL_ADD_ONS ) }


-- Sorting Rules
UiModWindow.SORT_ORDER_UP	   = 1
UiModWindow.SORT_ORDER_DOWN	   = 2

UiModWindow.MOD_SORTBY_NAME    = 1
UiModWindow.MOD_SORTBY_ENABLED = 2

function NewModSortData( param_label, param_title, param_desc )
    return { windowName=param_label, title=param_title, desc=param_desc }
end

UiModWindow.sortData = {}
UiModWindow.sortData[1] = NewModSortData( "UiModWindowSortButton1", GetPregameString( StringTables.Pregame.LABEL_MOD_NAME ), L"" )
UiModWindow.sortData[2] = NewModSortData( "UiModWindowSortButton2", GetPregameString( StringTables.Pregame.LABEL_ENABLED ), L"" )

UiModWindow.curSortType = UiModWindow.MOD_SORTBY_NAME
UiModWindow.curSortOrder = UiModWindow.SORT_ORDER_UP	


-- This function is used to compare mods for table.sort() on
-- the mod list display order.
local function CompareMods( index1, index2 )

    if( index2 == nil ) then
        return false
    end
    
    local sortType  = UiModWindow.curSortType
    local order     = UiModWindow.curSortOrder 
    
    --DEBUG(L"Sorting.. Type="..sortType..L" Order="..order )
    
    local mod1 = UiModWindow.modsList[ index1 ]
    local mod2 = UiModWindow.modsList[ index2 ]
    
    -- Sort By Name
    if( sortType == UiModWindow.MOD_SORTBY_NAME ) then
        return StringUtils.SortByString( mod1.wideStrName, mod2.wideStrName, order ) 
    end
    
    -- Sort By Enabled
    if( sortType == UiModWindow.MOD_SORTBY_ENABLED ) then
        if( mod1.isEnabled == mod2.isEnabled  ) then        
            return StringUtils.SortByString( mod1.wideStrName, mod2.wideStrName, UiModWindow.SORT_ORDER_UP )  
        else            
            if( order == UiModWindow.SORT_ORDER_UP ) then	
                return ( mod2.isEnabled )
            else
                return ( mod1.isEnabled )
            end		
        end
    end
end

local function SortModsList()

    local sortType  = UiModWindow.curSortType
    local order     = UiModWindow.curSortOrder 

    --DEBUG(L" Sorting Mods: type="..sortType..L" order="..order )
    table.sort( UiModWindow.modSetDisplayOrder, CompareMods )
end

local function UpdateModsList()
    
    ListBoxSetDisplayOrder("UiModWindowModsList", UiModWindow.modSetDisplayOrder )
end


local function UpdateModData()

    UiModWindow.modSets = {}
    UiModWindow.modsList = ModulesGetData()
   
    -- Build a list of all the mod sets
    local setNames = {}
    for modIndex, modData in ipairs( UiModWindow.modsList ) do
        
        
        local found = false

        if( setNames[ modData.setName ] == nil ) then
            setNames[ modData.setName ] = true
            table.insert( UiModWindow.modSets, modData.setName )
        end
        
        -- Create a w-string version of the mod name for display
        modData.wideStrName = StringToWString( modData.name )
                
        --DEBUG(L"Mod ["..modIndex..L"] = "..modData.wideStrName )
    end
    
end

local function GetModByName( name ) 

    if( UiModWindow.modsList == nil ) then
        return nil
    end
    
    for index, data in ipairs( UiModWindow.modsList ) do
        if( data.name == name ) then
            return data
        end    
    end
    
end


----------------------------------------------------------------
-- LoginWindow Functions
----------------------------------------------------------------

-- OnInitialize Handler()
function UiModWindow.Initialize()

    LabelSetText( "UiModWindowTitleBarText", GetPregameString( StringTables.Pregame.LABEL_UI_MOD_SETTINGS ) )   
   
     -- Mod Group Tabs
    for index, tabData in ipairs( UiModWindow.modGroupTabs ) do
        ButtonSetText( "UiModWindowModGroupTab"..index, tabData.name )
        ButtonSetStayDownFlag( "UiModWindowModGroupTab"..index, true )

        -- Hide the Tabs for now
        WindowSetShowing(  "UiModWindowModGroupTab"..index, false )
    end
   
    -- List Sort Buttons
    ButtonSetText( "UiModWindowSortButton1", GetPregameString( StringTables.Pregame.LABEL_MOD_NAME ) )
    ButtonSetText( "UiModWindowSortButton2", GetPregameString( StringTables.Pregame.LABEL_ENABLED ) )    
    
    -- No Mods Text
    LabelSetText("UiModWindowNoModsText", GetStringFromTable( "CustomizeUiStrings", StringTables.CustomizeUi.TEXT_NO_MODS_FOUND ) ) 
        
    -- Managment Buttons
    ButtonSetText( "UiModWindowEnableAllButton", GetStringFromTable( "CustomizeUiStrings", StringTables.CustomizeUi.UI_MODS_ENABLE_ALL_BUTTON ) ) 
    ButtonSetText( "UiModWindowDisableAllButton", GetStringFromTable( "CustomizeUiStrings", StringTables.CustomizeUi.UI_MODS_DISABLE_ALL_BUTTON ) )  
          
    -- Buttons
    ButtonSetText( "UiModWindowAdvancedButton", GetStringFromTable( "CustomizeUiStrings", StringTables.CustomizeUi.LABEL_ADVANCED_SETTINGS_BUTTON ) )  
    ButtonSetText( "UiModWindowOkayButton", GetPregameString( StringTables.Pregame.LABEL_OKAY ) )   
    ButtonSetText( "UiModWindowCancelButton", GetPregameString( StringTables.Pregame.LABEL_CANCEL ) )   
    
    -- Details    
    LabelSetText( "UiModWindowModDetailsScrollChildAuthorLabel", GetPregameString( StringTables.Pregame.TEXT_MOD_AUTHOR ) )
    LabelSetText( "UiModWindowModDetailsScrollChildVersionLabel", GetPregameString( StringTables.Pregame.TEXT_MOD_VERSION ) )
    LabelSetText( "UiModWindowModDetailsScrollChildGameVersionLabel", GetStringFromTable( "CustomizeUiStrings", StringTables.CustomizeUi.TEXT_MOD_GAME_VERSION ) )
    LabelSetText( "UiModWindowModDetailsScrollChildDescriptionLabel",GetPregameString( StringTables.Pregame.TEXT_MOD_DESCRIPTION ) )
       
    UiModWindow.UpdateInstructions()
    UiModWindow.InitAdvancedSettings()
end

function UiModWindow.OnShown()
    WindowUtils.OnShown()
    UpdateModData()
    
    UiModWindow.ShowModTab( UiModWindow.TAB_ADDONS_MODS )    
    UiModWindow.UpdateModSortButtons()
    UiModWindow.UpdateEnableDisableAllButtons()
end

function UiModWindow.OnHidden()
    WindowUtils.OnHidden()
    UiModWindow.modOptionsChanged = false
end

function UiModWindow.UpdateInstructions()

    -- If the AddOns directory is a relative path, this call will pre-pend the game's exe path
    local addOnsDir = FileUtils.GetFullPathWideString( SystemData.Directories.AddOnsInterface )
 
    local text = GetStringFormatFromTable( "CustomizeUiStrings", StringTables.CustomizeUi.TEXT_ADD_ONS_BASIC_INSTRUCTIONS, { addOnsDir } )

    -- Instructions
    LabelSetText( "UiModWindowInstructions", text )    
 
end

function UiModWindow.ShowModTab( tabId )        
      
    local setName = UiModWindow.modGroupTabs[ tabId ].name
    
    -- Update the Tabs
    for index, tabData in ipairs( UiModWindow.modGroupTabs ) do
        ButtonSetText( "UiModWindowModGroupTab"..index, tabData.name )
        ButtonSetPressedFlag( "UiModWindowModGroupTab"..index, index == tabId )
    end

    -- Update the List      
    UiModWindow.modSetDisplayOrder = {}
   
    local displayIndex = 1
    for index, modData in ipairs( UiModWindow.modsList ) do
    
         if( (setName == modData.setName) or (UiModWindow.Settings.showMainUiInModsList == true )  )
         then           
            UiModWindow.modSetDisplayOrder[displayIndex] = index
            displayIndex = displayIndex + 1
         end
    end
    
    SortModsList()
    UpdateModsList()
    
    UiModWindow.ShowModDetails( UiModWindow.modsList[ UiModWindow.modSetDisplayOrder[1] ] )
    
    -- Display the 'No Mods' Text if no mods have been found.
    WindowSetShowing( "UiModWindowNoModsText", UiModWindow.modSetDisplayOrder[1] == nil )
end

function UiModWindow.OnClickModListSortButton()
    local type = WindowGetId( SystemData.ActiveWindow.name )
    
    -- If we are already using this sort type, toggle the order.
    if( type == UiModWindow.curSortType ) then
        if( UiModWindow.curSortOrder == UiModWindow.SORT_ORDER_UP ) then
            UiModWindow.curSortOrder = UiModWindow.SORT_ORDER_DOWN
        else
            UiModWindow.curSortOrder = UiModWindow.SORT_ORDER_UP
        end
        
    -- Otherwise change the type and use the up order.	
    else
        UiModWindow.curSortType = type
        UiModWindow.curSortOrder = UiModWindow.SORT_ORDER_UP
    end

    SortModsList()
    UpdateModsList()
    
    UiModWindow.UpdateModSortButtons()
end


function UiModWindow.OnMouseOverModListSortButton()

end

-- Displays the clicked sort button as pressed down and positions an arrow above it
function UiModWindow.UpdateModSortButtons()

    local type = UiModWindow.curSortType
    local order = UiModWindow.curSortOrder

    
    for index, data in ipairs( UiModWindow.sortData ) do      
        ButtonSetPressedFlag( data.windowName, index == UiModWindow.curSortType )       
    end
    
    -- Update the Arrow
    WindowSetShowing( "UiModWindowSortUpArrow", order == UiModWindow.SORT_ORDER_UP )
    WindowSetShowing( "UiModWindowSortDownArrow", order == UiModWindow.SORT_ORDER_DOWN )
            
    local window = UiModWindow.sortData[type].windowName

    if( order == UiModWindow.SORT_ORDER_UP ) then		
        WindowClearAnchors( "UiModWindowSortUpArrow" )
        WindowAddAnchor("UiModWindowSortUpArrow", "right", window, "right", -8, 0 )
        
    else
        WindowClearAnchors( "UiModWindowSortDownArrow" )
        WindowAddAnchor("UiModWindowSortDownArrow", "right", window, "right", -8, 0 )
        
    end

end

function UiModWindow.MarkAsChanged()
    UiModWindow.modOptionsChanged = true
end

function UiModWindow.OnOkayButton()

    if( not UiModWindow.modOptionsChanged )
    then
    
        WindowSetShowing( "UiModWindow", false )
        return
    end    
    
    UiModWindow.CreateReloadDialogMods()
end

function UiModWindow.CreateReloadDialogMods()

    -- Otherwise pop up a dialog.
    DialogManager.MakeTwoButtonDialog(  GetStringFromTable( "CustomizeUiStrings", StringTables.CustomizeUi.TEXT_UI_MOD_SETTINGS_CHANGED_DIALOG ), 
                                            GetString( StringTables.Default.LABEL_YES ), UiModWindow.SaveAndReloadMods, 
                                            GetString( StringTables.Default.LABEL_NO ),  nil )
    
end

function UiModWindow.SaveAndReloadMods()

    WindowSetShowing( "UiModWindow", false )
    
    for index, modData in ipairs( UiModWindow.modsList ) do    
        ModuleSetEnabled( modData.name, modData.isEnabled  )
    end    

    BroadcastEvent( SystemData.Events.RELOAD_INTERFACE )
   
end

function UiModWindow.OnCancelButton()
    
    -- Close the window         
    WindowSetShowing( "UiModWindow", false )
end



function UiModWindow.OnClickModGroupTab()
    local tab = WindowGetId( SystemData.ActiveWindow.name )
    UiModWindow.ShowModTab( tab )  
end

function UiModWindow.OnMouseOverModGroupTab()

end

function UiModWindow.UpdateModRows()

    if (UiModWindowModsList.PopulatorIndices ~= nil) then				
        for rowIndex, dataIndex in ipairs (UiModWindowModsList.PopulatorIndices) do
        
            local modData = UiModWindow.modsList[ dataIndex ]
            UiModWindow.UpdateModRowByIndex( rowIndex, modData )				
        end
    end    

end

function UiModWindow.UpdateModRowByIndex( rowIndex, modData )
    local row_mod = math.mod (rowIndex, 2)
    local color = PregameDataUtils.GetAlternatingRowColor( row_mod )			
    local labelName = "UiModWindowModsListRow"..rowIndex.."Name"

    WindowSetTintColor("UiModWindowModsListRow"..rowIndex.."Background", color.r, color.g, color.b)		

    ButtonSetCheckButtonFlag( "UiModWindowModsListRow"..rowIndex.."Enabled", true )			
    ButtonSetPressedFlag( "UiModWindowModsListRow"..rowIndex.."Enabled", modData.isEnabled )	

        local color = DefaultColor.GetRowColor( rowIndex )		        
        DefaultColor.SetWindowTint( "UiModWindowModsListRow"..rowIndex.."Background",  color )		         	  
        
        ButtonSetCheckButtonFlag( "UiModWindowModsListRow"..rowIndex.."Enabled", true )			
        ButtonSetPressedFlag( "UiModWindowModsListRow"..rowIndex.."Enabled", modData.isEnabled )	
        
        -- Set the Text color based on selection
        color = { r=255, g=255, b=255 }
        if( UiModWindow.selectedModName == modData.name ) 
        then
            color = { r=255, g=204, b=102 }
        end
        LabelSetTextColor( "UiModWindowModsListRow"..rowIndex.."Name", color.r, color.g, color.b)
end

function UiModWindow.UpdateEnableDisableAllButtons()
    
    local numEnabled = 0
    local numDisabled = 0
    
    for _, dataIndex in ipairs( UiModWindow.modSetDisplayOrder ) 
    do    
        local modData = UiModWindow.modsList[ dataIndex ]
        if(  modData.isEnabled )
        then
           numEnabled = numEnabled + 1
        else
           numDisabled = numDisabled + 1
        end 
           	
    end    
    
    ButtonSetDisabledFlag( "UiModWindowEnableAllButton", numDisabled == 0 )
    ButtonSetDisabledFlag( "UiModWindowDisableAllButton", numEnabled == 0 )

end

function UiModWindow.OnEnableAllButton()

    if( ButtonGetDisabledFlag( SystemData.ActiveWindow.name ) )
    then
        return
    end

    for _, dataIndex in ipairs( UiModWindow.modSetDisplayOrder ) 
    do    
        local modData = UiModWindow.modsList[ dataIndex ]
        modData.isEnabled = true  	
    end   

    UiModWindow.MarkAsChanged()
    UiModWindow.UpdateEnableDisableAllButtons()
        
    UpdateModsList()
end

function UiModWindow.OnDisableAllButton()

    if( ButtonGetDisabledFlag( SystemData.ActiveWindow.name ) )
    then
        return
    end

    for _, dataIndex in ipairs( UiModWindow.modSetDisplayOrder ) 
    do    
        local modData = UiModWindow.modsList[ dataIndex ]       
        modData.isEnabled = false         
    end 
    
    UiModWindow.MarkAsChanged()  
    UiModWindow.UpdateEnableDisableAllButtons()    
    
    UpdateModsList()
end


function UiModWindow.OnToggleMod()

    local rowIndex = WindowGetId( WindowGetParent( SystemData.ActiveWindow.name ) )

    local dataIndex = ListBoxGetDataIndex( "UiModWindowModsList", rowIndex )
    
    local modData = UiModWindow.modsList[ dataIndex ]
    
    modData.isEnabled = ButtonGetPressedFlag( SystemData.ActiveWindow.name )
    
    UiModWindow.MarkAsChanged()    
    UiModWindow.UpdateEnableDisableAllButtons()
end

function UiModWindow.OnClickModRow()    
    local rowIndex = WindowGetId( SystemData.ActiveWindow.name )
    
    local dataIndex = UiModWindowModsList.PopulatorIndices[rowIndex]
    local modData = UiModWindow.modsList[ dataIndex ]
    
    UiModWindow.ShowModDetails( modData )
end

function UiModWindow.ShowModDetails( modData )
    
    UiModWindow.selectedModName = nil
    
    local name      = L""
    local author    = L""
    local version   = L""
    local gameVersion = L""
    local desc      = L""

    if( modData ~= nil ) 
    then    
        name    = modData.wideStrName            
        
        version = StringToWString( modData.version )
        
        if( modData.versionsettings.gameVersion )
        then     
            gameVersion = StringToWString( modData.versionsettings.gameVersion )
        end
        
        if( gameVersion == L"" )
        then
            gameVersion = GetStringFromTable( "CustomizeUiStrings", StringTables.CustomizeUi.VERSION_MISMATCH_GAME_VERSION_NOT_SPECIFIED )
        end            

        -- These fields may or may not be set on the Mod Data
        if( modData.author ) 
        then
            author = StringToWString( modData.author.name )
        end
        
        if( modData.description ) then
            desc = StringToWString( modData.description.text )
        end
        
        --DEBUG(L"Author: "..author)
        --DEBUG(L"Version: "..version)
        --DEBUG(L"Desc: "..desc)

        UiModWindow.selectedModName = modData.name
    end

    LabelSetText( "UiModWindowModDetailsScrollChildName", name )
    LabelSetText( "UiModWindowModDetailsScrollChildAuthorText", author  )
    LabelSetText( "UiModWindowModDetailsScrollChildVersionText", version  )
    LabelSetText( "UiModWindowModDetailsScrollChildGameVersionText", gameVersion  )
    LabelSetText( "UiModWindowModDetailsScrollChildDescriptionText",  desc  )
       
    UiModWindow.UpdateModRows()
       
    -- Hide the scroll contents when no mods are selected   
    WindowSetShowing( "UiModWindowModDetailsScrollChild", modData ~= nil )   
        
    ScrollWindowSetOffset( "UiModWindowModDetails", 0 )
    ScrollWindowUpdateScrollRect( "UiModWindowModDetails" )
end

--------------------------------------------------------------------
-- Advanced Settings

function UiModWindow.InitAdvancedSettings()
        
    CreateWindow("UiModAdvancedWindow", false )
    LabelSetText( "UiModAdvancedWindowTitleBarText", GetPregameString( StringTables.Pregame.LABEL_ADVANCED_MOD_SETTINGS ) )
    
    LabelSetText( "UiModAdvancedWindowInstructions", GetStringFromTable( "CustomizeUiStrings", StringTables.CustomizeUi.TEXT_UI_ADVANCED_SETTINGS_WARNING ) )   
    
    -- EA Default / Custom Toggle 
    LabelSetText( "UiModAdvancedWindowUseEADefaultToggleLabel", GetStringFromTable( "CustomizeUiStrings", StringTables.CustomizeUi.TEXT_LOAD_EA_DEFAULT_UI ) )
    ButtonSetStayDownFlag( "UiModAdvancedWindowUseEADefaultToggleButton", true )   

    LabelSetText( "UiModAdvancedWindowUseCustomUIToggleLabel", GetStringFromTable( "CustomizeUiStrings", StringTables.CustomizeUi.TEXT_REPLACE_EA_DEFAULT ) )
    ButtonSetStayDownFlag( "UiModAdvancedWindowUseCustomUIToggleButton", true )   
    
    --- Toggle 
    LabelSetText( "UiModAdvancedWindowShowInAddOnsListCheckLabel", GetStringFromTable( "CustomizeUiStrings", StringTables.CustomizeUi.TEXT_SHOW_MAIN_UI_IN_MODS_LIST ) )
    ButtonSetStayDownFlag( "UiModAdvancedWindowShowInAddOnsListCheckButton", true )  

    -- Add Ons Directory
    LabelSetText( "UiModAdvancedWindowCustomUITitle", GetPregameString( StringTables.Pregame.LABEL_MAIN_INTERFACE  ) )
    
    LabelSetText( "UiModAdvancedWindowAddOnsTitle", GetStringFromTable( "CustomizeUiStrings", StringTables.CustomizeUi.LABEL_ADD_ONS_DIRECTORY ) )   

    ButtonSetText( "UiModAdvancedWindowDebugWindowButton", GetStringFromTable( "CustomizeUiStrings", StringTables.CustomizeUi.LABEL_DEBUG_LOG_BUTTON ) )   
    ButtonSetText( "UiModAdvancedWindowOkayButton", GetPregameString( StringTables.Pregame.LABEL_OKAY ) )   
    ButtonSetText( "UiModAdvancedWindowCancelButton", GetPregameString( StringTables.Pregame.LABEL_CANCEL ) )   

end

function UiModWindow.UpdateAdvancedSettings()
    
    -- EA Default / Custom Path Toggle
    ButtonSetPressedFlag( "UiModAdvancedWindowUseEADefaultToggleButton", not SystemData.Settings.UseCustomUI )
    ButtonSetPressedFlag( "UiModAdvancedWindowUseCustomUIToggleButton", SystemData.Settings.UseCustomUI )
           
    TextEditBoxSetText( "UiModAdvancedWindowCustomUiDirectory", SystemData.Directories.CustomInterface )
        
    ButtonSetPressedFlag( "UiModAdvancedWindowShowInAddOnsListCheckButton", UiModWindow.Settings.showMainUiInModsList )

    -- Add Ons
    TextEditBoxSetText( "UiModAdvancedWindowAddOnsDirectory", SystemData.Directories.AddOnsInterface )         
end

function UiModWindow.SelectMainUiLoadOption()
    
    if( SystemData.ActiveWindow.name == "UiModAdvancedWindowUseEADefaultToggle" )
    then  
        UiModWindow.SetLoadCustomUI( false )      
    else    
        UiModWindow.SetLoadCustomUI( true )
    end

end

function UiModWindow.SetLoadCustomUI( value )
    ButtonSetPressedFlag( "UiModAdvancedWindowUseEADefaultToggleButton", not value )
    ButtonSetPressedFlag( "UiModAdvancedWindowUseCustomUIToggleButton", value )  
end


function UiModWindow.OnAdvancedButton()
    local showing = WindowGetShowing("UiModAdvancedWindow")
    WindowSetShowing("UiModAdvancedWindow", not showing )
end

function UiModWindow.OnAdvancedShown()
    WindowUtils.OnShown()
    UiModWindow.UpdateAdvancedSettings()
end

function UiModWindow.OnAdvancedHidden()
    WindowUtils.OnHidden()
end


function UiModWindow.ToggleShowMainUiInModsList()
    UiModWindow.Settings.showMainUiInModsList = not UiModWindow.Settings.showMainUiInModsList
    ButtonSetPressedFlag( "UiModAdvancedWindowShowInAddOnsListCheckButton", UiModWindow.Settings.showMainUiInModsList )
    UiModWindow.ShowModTab( UiModWindow.TAB_ADDONS_MODS )   
end

function UiModWindow.OnDebugWindowButton()
    local showing = WindowGetShowing("DebugWindow")
    WindowSetShowing("DebugWindow", not showing )
end

function UiModWindow.OnAdvancedOkayButton()

    local optionsChanged = false
    
    local useCustom = ButtonGetPressedFlag( "UiModAdvancedWindowUseCustomUIToggleButton" )
    if( useCustom ~= SystemData.Settings.UseCustomUI )
    then
       optionsChanged = true
    end
    
    -- Custom Ui Directory
    local oldText  = SystemData.Directories.CustomInterface
    local newText  = UiModAdvancedWindowCustomUiDirectory.Text
    if( oldText ~= newText )
    then
        optionsChanged = true
    end 
    
    -- Add Ons Directory
    local oldText  = SystemData.Directories.AddOnsInterface
    local newText  = UiModAdvancedWindowAddOnsDirectory.Text
    if( oldText ~= newText )
    then
        UiModWindow.UpdateInstructions()
        optionsChanged = true
    end 
    
    if( (optionsChanged == false) and (UiModWindow.isInErrorMode == false) )
    then        
        WindowSetShowing( "UiModAdvancedWindow", false )
        return
    end
    
    UiModWindow.CreateReloadDialogAdvanced()
end

function UiModWindow.OnAdvancedCancelButton()

    WindowSetShowing("UiModAdvancedWindow", false )
    
    if( UiModWindow.isInErrorMode )
    then
        UiModWindow.ShowCustomUIErrorMessage()
    end
end


function UiModWindow.CreateReloadDialogAdvanced()

    -- Otherwise pop up a dialog.
    DialogManager.MakeTwoButtonDialog(  GetStringFromTable( "CustomizeUiStrings", StringTables.CustomizeUi.TEXT_UI_MOD_SETTINGS_CHANGED_DIALOG ), 
                                            GetString( StringTables.Default.LABEL_YES ), UiModWindow.SaveAndReloadAdvanced, 
                                            GetString( StringTables.Default.LABEL_NO ),  UiModWindow.CancelAdvancedDialog )
    
end

function UiModWindow.SaveAndReloadAdvanced()

    -- Close the window     
    WindowSetShowing( "UiModAdvancedWindow", false )

    SystemData.Settings.UseCustomUI = ButtonGetPressedFlag( "UiModAdvancedWindowUseCustomUIToggleButton" )

    SystemData.Directories.CustomInterface = UiModAdvancedWindowCustomUiDirectory.Text
    SystemData.Directories.AddOnsInterface = UiModAdvancedWindowAddOnsDirectory.Text
          

    BroadcastEvent( SystemData.Events.USER_SETTINGS_CHANGED )
    BroadcastEvent( SystemData.Events.RELOAD_INTERFACE )
   
end

function UiModWindow.CancelAdvancedDialog()
    if( UiModWindow.isInErrorMode and WindowGetShowing("UiModAdvancedWindow") == false )
    then
        UiModWindow.ShowCustomUIErrorMessage()
    end
end

function UiModWindow.ShowCustomUIErrorMessage()

    UiModWindow.isInErrorMode = true

    local dir  = SystemData.Directories.CustomInterface
    local text = GetStringFormatFromTable( "CustomizeUiStrings", StringTables.CustomizeUi.TEXT_CUSTOM_UI_DIRECTORY_ERROR, { dir }  )

    -- Otherwise pop up a dialog.
    DialogManager.MakeTwoButtonDialog(  text,
                                        GetStringFromTable( "CustomizeUiStrings", StringTables.CustomizeUi.LABEL_REVERT ), UiModWindow.RevertToEADefaultData, 
                                        GetString( StringTables.Default.LABEL_CANCEL ),  UiModWindow.ShowAdvancedDialog )
    
end

function UiModWindow.RevertToEADefaultData()
    UiModWindow.UpdateAdvancedSettings()  
    UiModWindow.SetLoadCustomUI( false )
    UiModWindow.CreateReloadDialogAdvanced()
end

function UiModWindow.ShowAdvancedDialog()
    WindowSetShowing("UiModAdvancedWindow", true )
end
